package com.yiqi.lottery.feature.find.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.CircleBo;
import com.yiqi.lottery.entity.bo.FoucsBo;
import com.yiqi.lottery.entity.bo.HallBo;
import com.yiqi.lottery.feature.find.contract.CircleContract;
import com.yiqi.lottery.feature.find.dependencies.circle.CirclePresenterModule;
import com.yiqi.lottery.feature.find.dependencies.circle.DaggerCircleComponent;
import com.yiqi.lottery.feature.lottery.view.CommonWebActivity;
import com.yiqi.lottery.feature.lottery.view.adapter.WinningAdapter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GameAnalysisActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, CircleContract.View, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_winning)
    RecyclerView mRvWinning;

    private WinningAdapter mAdapter;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    @Inject
    CircleContract.Presenter mPresenter;
    private EmptyView mEmptyView;
    PageData<CircleBo> mData;
    private int type;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_game_analysis;
    }

    @Override
    protected void initInjector() {
        DaggerCircleComponent.builder().circlePresenterModule(new CirclePresenterModule(this)).build().inject(this);
    }



    @Override
    protected void initData() {
        type=this.getIntent().getIntExtra("type",4);
        mPresenter.doGetCircleList(pageIndex, type, 0);
        initToolBar(mToolbar, type==4?"中奖专栏":"比赛分析", true, true);
    }
    private void initAdapter() {
        mAdapter = new WinningAdapter();
        mEmptyView = new EmptyView(mRvWinning);
        mRvWinning.setLayoutManager(new LinearLayoutManager(this));
        mRvWinning.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter.bindToRecyclerView(mRvWinning);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnLoadMoreListener(this);
    }
    @Override
    protected void initView() {
        initAdapter();

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent=new Intent(this,TextActivity.class);
        intent.putExtra("text",mAdapter.getData().get(position).getPost_content());
        intent.putExtra("id",mAdapter.getData().get(position).getPost_id()+"");
        startActivity(intent);
    }


    @Override
    public void showDetailSuccess(FoucsBo foucsBo) {

    }


    @Override
    public void showCircleListSuccess(PageData<CircleBo> circleBoPageData) {
        mData=circleBoPageData;
        mAdapter.setNewData(circleBoPageData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showCircleListMoreSuccess(PageData<CircleBo> circleBoPageData) {
        isErr = false;
        mAdapter.addData(circleBoPageData.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showCircleMoreErrorMsg(String msg) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showCircleListError(String error) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
    }

    @Override
    public void showCircleListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void showDaiaZanSuccess(String msg) {

    }

    @Override
    public void showDaiaZanError(String msg) {

    }

    @Override
    public void onLoadMoreRequested() {

        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    mPresenter.doGetCircleMoreList(pageIndex, type, 0);

                }
            }

        }
    }
}
